package com.kaikai.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.commonutils.R;
import com.kaikai.edu.client.VodClient;
import com.kaikai.edu.entity.EduVideo;
import com.kaikai.edu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    VodClient vodClient;
    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo video){
        eduVideoService.save(video);
        return R.ok();
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo video){
        eduVideoService.updateById(video);
        return R.ok();
    }
    //查询小节
    @GetMapping("{id}")
    public R getVideo(@PathVariable String id){
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("id", id);
        EduVideo video = eduVideoService.getOne(wrapper);
        return R.ok().data("video",video);
    }
    //删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if(StringUtils.hasText(videoSourceId)){
             vodClient.removeVideo(videoSourceId);
        }
        eduVideoService.removeById(id);
        return R.ok();
    }
    //获取播放videoId
    @GetMapping("getVideoId/{id}")
    public R getVideoIdById(@PathVariable String id){
        String videoId=eduVideoService.getVideoIdById(id);
        if(StringUtils.isEmpty(videoId)){
            return R.error().message("暂无视频");
        }
        return R.ok().data("videoId",videoId);
    }
}

